
import 'dart:io';

import 'package:baselib/utils/gallery_camera_pick_util.dart';
import 'package:baselib/utils/image_util.dart';
import 'package:baselib/utils/keyboard_util.dart';
import 'package:baselib/utils/load_util.dart';
import 'package:baselib/utils/log_util.dart';
import 'package:baselib/utils/md5_util.dart';
import 'package:baselib/utils/navigator_util.dart';
import 'package:baselib/utils/px_base_util.dart';
import 'package:baselib/utils/toast_util.dart';
import 'package:baselib/widgets/dialog_util.dart';
import 'package:flutter/material.dart';
import 'package:framelib/base/base_view_model.dart';
import 'package:framelib/config/api_config.dart';
import 'package:framelib/net/baseentity.dart';
import 'package:framelib/utils/oss_util.dart';
import 'package:framelib/utils/string_util.dart';
import 'package:module_business/module_index/model/voice_chat_tag_entity.dart';
import 'package:module_business/module_index/service/module_index_service.dart';
import 'package:module_business/module_index/view/create_voice_room_widget.dart';
import 'package:get/get.dart';

class CreateVoiceRoomViewModel extends BaseViewModel<ModuleIndexService>{

  ModuleIndexService moduleIndexService;

  String coverPath='';
  File coverFile;
  String roomName='';
  String roomPassword='';
  String roomRemark='';
  List<VoiceChatTagEntity> voiceChatTags = [];

  RxInt selectedTagIndex = (-1).obs;
  RxBool isSetPassword = false.obs;

  @override
  ModuleIndexService createService() =>moduleIndexService = ModuleIndexService();

  @override
  void onInit() {
    super.onInit();
    //获取房间标签
    getIndexTags();
  }
  @override
  void onClose() {
    super.onClose();
  }
  void getIndexTags() async{
    BaseEntity baseEntity = await moduleIndexService.api_voicechat_tag_list();
    if(baseEntity?.code == ApiConfig.SUCCESS_CODE){
      List<VoiceChatTagEntity> results = baseEntity.result??[];
      if(results.isEmpty)return;
      var result = results[0];
      if(result==null)return;
      var tagChildrens = result.children??[];
      if(tagChildrens.isEmpty)return;
      voiceChatTags = tagChildrens;
      update();
    }

  }
  selectCover() async{
    //https://app-bucket-test.oss-accelerate.aliyuncs.com/ios/77985415/230308/u1023329454(960x1280).jpg
    coverFile = await GalleryCameraPickUtil.instance.selectPicture();
    if(coverFile==null)return;
    LoadUtil.showLoad(loadingText: '${StringUtil.instance.Uploading}...');
    OssUtil.instance.upload(coverFile, complete: (path,isUpload){
      LoadUtil.hideLoad();
      coverPath = path;
      LogUtil.d('selectCover-->coverPath=$coverPath');
      update();
    });
  }

  roomNameChange(String value) {
    roomName = value;
  }

  roomPasswordChange(String value) {
    roomPassword = value;
  }
  remarkChange(value) {
    roomRemark = value;
  }

  bool isValid() {
    if((coverPath??'').isEmpty){
      ToastUtil.toast(StringUtil.instance.Please_upload_the_cover);
      return false;
    }
    if((roomName??'').isEmpty){
      ToastUtil.toast(StringUtil.instance.Please_input_the_Room_name);
      return false;
    }
    if(selectedTagIndex.value==(-1)){
      ToastUtil.toast(StringUtil.instance.Please_select_the_Room_tag);
      return false;
    }
    if((roomRemark??'').isEmpty){
      ToastUtil.toast(StringUtil.instance.Set_up_your_room_profile_d);
      return false;
    }
    return true;
  }

  tagItemClick(int index) {
    selectedTagIndex.value = index;
  }

  setPassword(bool isSetPasswordTemp) {
    isSetPassword..value = isSetPasswordTemp;
  }
}
